package com.sg.service.biz.framework.nb;

import com.alibaba.fastjson.JSONArray;
import com.sg.common.util.CommonFunctionHelper;
import com.sg.common.util.RedisUtil;
import com.sg.dto.biz.framework.req.SetSpaceInfoToRedisReqDto;
import com.sg.dto.biz.framework.res.ArchiveSingleDataComRespDto;
import com.sg.dto.biz.framework.res.SetSpaceInfoToRedisRespDto;
import com.wicket.okrcomponent.common.annotations.BanAuto;
import com.wicket.okrframework.common.exception.BizException;
import org.apache.skywalking.apm.toolkit.trace.Tag;
import org.apache.skywalking.apm.toolkit.trace.Tags;
import org.apache.skywalking.apm.toolkit.trace.Trace;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.sql.DataSource;
import java.sql.SQLException;


/**
 * @author ： 手工接入方法
 * @version 1.0
 * @since 2022/5/28 15:32
 */
@Service
public class NbCommon {
    @Resource
    RedisUtil redisClient;
    /**
     * code:deleteSingleDataCom
     * name:删除单条数据
     * desc:undefined
     **/

    @Autowired
    private DataSource dataSource;

    @BanAuto
    @Trace(operationName = "删除单条数据")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    public DeleteSingleDataComRespDto deleteSingleDataCom(DeleteSingleDataComReqDto reqDto) {
        DeleteSingleDataComRespDto retData = new DeleteSingleDataComRespDto();
        try {
            CommonFunctionHelper.nbDbWorker(dataSource.getConnection(), reqDto, "deleteSingleDataCom", null);
        } catch (SQLException e) {
            throw new BizException("-1", e.getMessage(), false);
        }
        return retData;
    }

    /**
     * code:archiveSingleDataCom
     * name:存档单条数据
     * desc:undefined
     **/
    @BanAuto
    @Trace(operationName = "存档单条数据")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    public ArchiveSingleDataComRespDto archiveSingleDataCom(ArchiveSingleDataComReqDto reqDto) {
        ArchiveSingleDataComRespDto retData = new ArchiveSingleDataComRespDto();
        try {
            CommonFunctionHelper.nbDbWorker(dataSource.getConnection(), reqDto, "archiveSingleDataCom", null);
        } catch (SQLException e) {
            throw new BizException("-1", e.getMessage(), false);
        }
        return retData;
    }

    /**
     * code:putSpaceInfoToRedis
     * name:空间信息存储Redis
     * desc:undefined
     **/
    @BanAuto
    @Trace(operationName = "空间信息存储Redis")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    public SetSpaceInfoToRedisRespDto setSpaceInfoToRedis(SetSpaceInfoToRedisReqDto reqDto) {
        SetSpaceInfoToRedisRespDto retData = new SetSpaceInfoToRedisRespDto();
        Object jo = JSONArray.toJSON(reqDto);
        redisClient.set(reqDto.getSpaceId(), jo.toString());
        return retData;
    }

    /**
     * code:batchUpdateSortNumCom
     * name:批量修改排序
     * desc:undefined
     **/
    @BanAuto
    @Trace(operationName = "批量修改排序")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    public BatchUpdateSortNumRespDto batchUpdateSortNum(BatchUpdateSortNumReqDto reqDto) {
        BatchUpdateSortNumRespDto retData = new BatchUpdateSortNumRespDto();
        for (ObjAttributeIdDto one : reqDto.getObjAttributeIdList()) {
            try {
                CommonFunctionHelper.nbDbWorker(dataSource.getConnection(), one, "updateSortNumCom", null);
            } catch (SQLException e) {
                throw new BizException("-1", "deleteSingleDataCom 操作错误", false);
            }
        }

        return retData;
    }
    // 手工接入方法
}
